package org.restorator.controller.security;

import org.restorator.entity.User;
import org.restorator.entity.dto.UserDto;
import org.restorator.service.UserService;
import org.restorator.service.impl.UserServiceImpl;
import org.restorator.utill.GeneralUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static org.restorator.utill.ControllerUtil.*;

/**
 * @author Snisar Bogdan.
 */
@WebServlet(name = "user", urlPatterns = "/user")
public class UserAllController extends HttpServlet {

    private static Logger logger = LoggerFactory.getLogger(GeneralUtil.getClassName());

    @Override
    public void init() throws ServletException {
        super.init();
        userService = new UserServiceImpl();
    }

    private UserService userService;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<User> users = userService.getAll();
            if(users != null && !users.isEmpty()) {
                Collection<UserDto> dto = userService.toDto(users);
                printOk(resp, dto);
            } else {
                printError(resp);
            }
        } catch (Exception e) {
            printError(resp);
        }
    }
}
